﻿using System;
using System.Linq;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using ComApp.Model;
using ComApp;

namespace AndroidApp
{
    [Activity(Label = "SiAi", MainLauncher = true, Icon = "@drawable/icon", Theme = "@android:style/Theme.NoTitleBar")]
    public class FirstView : Activity
    {
        private void InitApplication()
        {
            ThisApp.Instance.Start();
            AppUnity.Instance = new AppUnityInstance();  //初始化所有服务及单例对象
            AppUnity.Instance.LogService.Clear();

            AppUnity.Instance.Start();
            AppUnity.Instance.LogService.Log("AppUnity.Instance.Initialize成功");

        }

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            this.RequestWindowFeature(WindowFeatures.NoTitle);
            SetContentView(Resource.Layout.FirstView);
            Button openButton = FindViewById<Button>(Resource.Id.startHome);
            openButton.Click += (s, e) =>
            {
                Intent intent = new Intent(this, typeof(UploadView));
                intent.SetFlags(ActivityFlags.BroughtToFront);
                StartActivity(intent);
                //Intent startMain = new Intent(this, typeof(HomeView));//Intent.ActionMain,);
                //startMain.SetAction(Intent.ActionMain);
                //startMain.AddCategory(Intent.CategoryHome);
                //startMain.SetFlags(ActivityFlags.NewTask);
                //StartActivity(startMain); 
            };

            try
            {
                this.InitApplication();
                AppUnity.Instance.LogService.Log("Open FirstView成功");
                ThisApp.Instance.ViewManager.RegisterActivity(this);
                openButton.PerformClick();
            }
            catch (Exception exception)
            {
                FileLog.Instance.Log(exception);
            }
        }


        /// <summary>
        /// 从任务栏移走，会CloseBeforeExit，但不会提示
        /// 回退到桌面会提示退出，相当于应用退出 
        /// 隐藏到后台重新激活（从任务栏或桌面），会刷新，但没有退出过
        /// 还需要测试打开另一个Activity的情况
        /// </summary>
        bool CloseBeforeExit()
        {

            //Widget.HttpImageView.DeleteCacheFileAllAsync().ContinueWith(t =>
            //{
            //    if (!t.IsFaulted && !t.IsCanceled)
            //    {
            //        RunOnUiThread(() =>
            //        {
            //            Toast.MakeText(this, "删除成功完成", ToastLength.Long).Show();
            //        });

            //    }
            //});

            return true;
        }


        protected override void OnResume()
        {
            base.OnResume();
            AppUnity.Instance.LogService.Log("FirstView OnResume");
        }


        protected override void OnDestroy()
        {
            if (this.CloseBeforeExit())
            {
                AppUnity.Instance.LogService.Log(string.Format("关闭View={0}", this.GetType().FullName));
                ThisApp.Instance.ViewManager.UnRegisterActivity(this);
                base.OnDestroy();
            }
        }


    }
}

